﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });
});

function Submit() {

    $("#submit").hide();

    var input = {
        ID: $("#id").val(),
        Name: $("#Name").val(),
        Street: $("#Street").val(),
        Street2: $("#Street2").val(),
        City: $("#City").val(),
        State: $("#State").val(),
        ZipCode: $("#ZipCode").val(),
        Latitude: $("#Latitude").val(),
        Longitude: $("#Longitude").val(),
        DisplayOrder: $("#DisplayOrder").val(),
        FridayNote: $("#FridayNote").val(),
        LongNote: $("#LongNote").val()
    };

    Ajax({
        url: "/TarawehPrayers/SaveLocation",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                location.href = "/TarawehPrayers?msg=1";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}

$(function () {
    $('#loadgeo').click(function () {
        $('#loadgeo').val("getting geo location....");
        var address = $("#Street").val() + ' '
                    + $("#City").val() + ' '
                    + $("#State").val() + ' '
                    + $("#ZipCode").val();
        geocoder = new GClientGeocoder();
        geocoder.getLocations(address, ShowGeo);
    });
});
        
function ShowGeo(response) {
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } 
    else 
    {
        place = response.Placemark[0];
        $("#Latitude").val(place.Point.coordinates[1]);
        $("#Longitude").val(place.Point.coordinates[0]);
        $('#loadgeo').val("got it!");
        setTimeout(function () {
            $('#loadgeo').val("load from address again");
        }, 3000)
    }
}